package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSession;

import model.Language;
import model.User;
import DAO.LanguageDao;
import DAO.UserDao;



public class QueStudy_MemberController extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String uri = request.getRequestURI();
		String command = uri.substring(uri.lastIndexOf("/") + 1);
		
		ActionForward forward=null;
		Action action=null;

		
		if (command != null && command.trim().equals("Join.Member")) {
			forward =  new ActionForward();
			
			LanguageDao languageDao = new LanguageDao();
			request.setAttribute("langList",languageDao.getAllLanguage());
			
			forward.setPath("/joinus.jsp");

		}
		
		else if (command != null && command.trim().equals("Login.Member")) {
			forward =  new ActionForward();
			forward.setPath("/login.jsp");

		}
	
		
		else if (command != null && command.trim().equals("Logout.Member")) {
			forward =  new ActionForward();
		
			HttpSession session=request.getSession();
			session.invalidate();
			forward.setPath("List.Study");
			forward.setRedirect(true);
		}
		
		else if (command != null && command.trim().equals("Mypage.Member")) {
			forward =  new ActionForward();
			forward.setPath("/mypage.jsp");

		}
		else if (command != null && command.trim().equals("MyInfo.Member")) {
			HttpSession session=request.getSession();
			User user = new User();
			UserDao userDao = new UserDao();
			ArrayList <Language> languageList = new ArrayList <Language>();
			LanguageDao languageDao = new LanguageDao();
		
			int userId = session.getAttribute("userid") != null ? Integer
					.parseInt((String) session.getAttribute("userid")) : 0;

			if(userId<0)
			{
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");	
			}
			languageList=languageDao.getAllLanguage();
						
			request.setAttribute("user", userDao.getUserInfo(userId));	
			request.setAttribute("langList",languageList);
			forward =  new ActionForward();

			forward.setPath("/mypage_userInfo.jsp");

		}
		else if (command != null && command.trim().equals("Payment.Member")) {
			forward =  new ActionForward();
			HttpSession session=request.getSession();
			User user = new User();
			UserDao userDao = new UserDao();
		
			int userId = session.getAttribute("userid") != null ? Integer
					.parseInt((String) session.getAttribute("userid")) : 0;

			
			if(userId<0)
			{
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");	
			}
		
			int point = userDao.getUserInfo(userId).getPoint();
						request.setAttribute("point", point);
			
			forward.setPath("/mypage_chargePage.jsp");

		}
		else if (command != null && command.trim().equals("Modify.Member")) {
			HttpSession session=request.getSession();
			User user = new User();
			UserDao userDao = new UserDao();
			LanguageDao langDao = new LanguageDao();
		
			int userId = session.getAttribute("userid") != null ? Integer
					.parseInt((String) session.getAttribute("userid")) : 0;

			if(userId<0)
			{
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");	
			}
		
						
			request.setAttribute("user", userDao.getUserInfo(userId));	
			request.setAttribute("langList", langDao.getAllLanguage());
			forward =  new ActionForward();
			forward.setPath("/mypage_modify_userInfo.jsp");

		}
	
		else if (command != null && command.trim().equals("JoinAction.Member")) {
			action = new MemberJoinAction();
			try{
				
				forward=action.execute(request,response);
				
			}catch(Exception e)
			{
				e.printStackTrace();
			}

		}
	
		else if (command != null
				&& command.trim().equals("LoginAction.Member")) {
			action = new MemberLoginAction();
			try{
				forward=action.execute(request,response);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		else if (command != null
				&& command.trim().equals("LogoutAction.Member")) {
			
			try{
				HttpSession session=request.getSession();
				session.invalidate();

				forward =  new ActionForward();
				forward.setPath("questudy.jsp");
				forward.setRedirect(true);
				
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		else if (command != null
				&& command.trim().equals("IDCheckAction.Member")) {
			
			action = new MemberIDCheckAction();
			try{
				forward=action.execute(request,response);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		else if (command != null && command.trim().equals("ModifyAction.Member")) {
			action = new MemberModifyAction();
			try{
				
				forward=action.execute(request,response);
				
			}catch(Exception e)
			{
				e.printStackTrace();
			}

		}
		else if (command != null && command.trim().equals("PaymentAction.Member")) {
			action = new MemberPaymentAction();
			try{
				
				forward=action.execute(request,response);
				
			}catch(Exception e)
			{
				e.printStackTrace();
			}

		}
		/*
		else if (command != null && command.trim().equals("MemberIDCheckActio")) {
			action = new MemberJoinAction();
			try{
				forward=action.execute(request,response);
			}catch(Exception e)
			{
				e.printStackTrace();
			}

		}
*/
		
		
		if(forward !=null){
			if(forward.isRedirect()){
			response.sendRedirect(forward.getPath());	
				
			}else{
				RequestDispatcher dispatcher = 
						request.getRequestDispatcher(forward.getPath());
				System.out.println(dispatcher.toString());
				
				dispatcher.forward(request,response);
			}
		}
	}

}
